layui.use(['form', 'layer', 'util'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,util = layui.util;

    //倒计时
    var endtime = new Date($(".endtime").val()).getTime() //假设为结束日期
    ,serverTime = new Date().getTime(); //假设为当前服务器时间，这里采用的是本地时间，实际使用一般是取服务端的

    util.countdown(endtime, serverTime, function(date, serverTime, timer){
        var str = util.digit(date[1], 2) + ' : ' +  util.digit(date[2], 2) + ' : ' + util.digit(date[3], 2);
        $(".timespan").html(str);
    });

    $(function() {
        form.on('submit(formDemo)', function(data) {

            //将传入的数据转换成list集合
            var answerList = new Array();
            $.each( data.field , function(i, n){
                var index =i.indexOf("\_");
                var qid = i.substring(index + 1,i.length);
                var multiple_ = i.substring(0,index + 1);
                if (multiple_ == "multiple_"){
                    var arr = new Array();
                    $("input:checkbox[name=" + i + "]:checked").each(function(x){
                        arr[x] = $(this).val();
                    });
                    data.field[i] = arr.join(",");
                }
                answerList.push({
                    id: qid,
                    answer: data.field[i]
                });
            });

            //交卷参数  answerList(userAnwser)
            $.ajax({
                url: "/exam/submitPaper",
                type: "post",
                async: false,
                data:{
                    pId: $('.pId').val(),
                    uId: $('.uId').val(),
                    answerList:JSON.stringify(answerList)
                },
                success:function (res) {
                    if (res.code == 200) {
                        layer.tips(res.msg, '#butt', {
                            tips: [1, '#3595CC'],
                            time: 1500
                        });
                        $("#butt").attr("disabled","disabled");
                        setTimeout(function () {
                            window.location.href='/userPaperList';
                        }, 1500);
                    } else if (res == 400) {
                        //判断是否交过试卷，防止重复提交
                        layer.tips(res.msg, '#butt', {
                            tips: [1, '#FF5722'],
                            time: 1500});
                        setTimeout(function () {
                            window.location.href='/userPaperList';
                        }, 1500);

                    } else {
                        layer.tips(res.msg, '#butt', {
                            tips: [1, '#FF5722'],
                            time: 1500});
                    }

                }
            });

            return false;
        });

    });


    //自定义验证规则
    form.verify({
        otherReq: function(value,item){
            var $ = layui.$;
            var verifyName=$(item).attr('name')
                , verifyType=$(item).attr('type')
                ,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
                ,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
                ,isTrue= verifyElem.is(':checked')//是否命中校验
                ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
            if(!isTrue || !value){
                //定位焦点
                focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
                //对非输入框设置焦点
                focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
                    focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
                }).focus();
                layer.tips('必填项不能为空', '#butt', {
                    tips: [1, '#FF5722'],
                    time: 1500});
                return '必填项不能为空';
            }
        }
    });

});
